/*
 * Copyright 2018-10-14 21:39:18 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.erp.core.impl;

import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.lap.scd.commons.core.exception.ManagerException;
import com.lap.scd.commons.page.Page;
import com.lap.scp.erp.core.MaterielDetailManager;
import com.lap.scp.erp.dal.MaterielDAO;
import com.lap.scp.erp.dal.MaterielDetailDAO;
import com.lap.scp.erp.dal.PaymentDAO;
import com.lap.scp.erp.dal.PurchaseOrdersDAO;
import com.lap.scp.erp.dal.SellOrdersDAO;
import com.lap.scp.erp.domain.MaterielDetailDO;
import com.lap.scp.erp.domain.PaymentDO;
import com.lap.scp.erp.domain.PurchaseOrdersDO;
import com.lap.scp.erp.domain.SellOrdersDO;
import com.lap.scp.erp.query.MaterielDetailQueryVO;

/**
 * <pre>
 * 物料明细明细信息业务代码默认实现类，这里提供最小元素原子操作，所以尽量少在该类中做流程代码。
 * 业务流程代码应该写在AO中。
 * </pre>
 * 
 * @author 劳水生 Exp
 * @Date 2018-10-14 21:39:18
 * @since 2.0.0
 */
@Service("materielDetailManager")
public class DefaultMaterielDetailManager implements MaterielDetailManager {

	@Resource
	private MaterielDetailDAO materielDetailDAO;

	@Resource
	private PurchaseOrdersDAO purchaseOrdersDAO;

	@Resource
	private SellOrdersDAO sellOrdersDAO;

	@Resource
	private MaterielDAO materielDAO;

	@Resource
	private PaymentDAO paymentDAO;

	@Override
	public void insertMaterielDetail(MaterielDetailDO detailDO, PurchaseOrdersDO purchaseDO) throws ManagerException {
		materielDetailDAO.insertMaterielDetail(detailDO);
		if (detailDO.getId() > 0) {
			purchaseDO.setMaterielDetailId(detailDO.getId());
			purchaseOrdersDAO.insertPurchaseOrders(purchaseDO);

			materielDAO.updateQuantity(detailDO.getMaterielId(), detailDO.getQuantity(), detailDO.getUpdatedBy());
		}
	}

	@Override
	public int deleteById(Integer materielDetailId) throws ManagerException {
		return materielDetailDAO.deleteById(materielDetailId);
	}

	@Override
	public MaterielDetailDO queryById(Integer materielDetailId) throws ManagerException {
		return materielDetailDAO.queryById(materielDetailId);
	}

	@Override
	public List<MaterielDetailDO> queryByMaterielId(Integer materielId) throws ManagerException {
		return materielDetailDAO.queryByMaterielId(materielId);
	}

	@Override
	public List<MaterielDetailDO> queryPage(Page page, MaterielDetailQueryVO queryVO) throws ManagerException {
		int count = materielDetailDAO.countPage(queryVO);
		if (count == 0) {
			return Collections.emptyList();
		} else {
			page.setTotalItem(count);
			return materielDetailDAO.queryPage(page, queryVO);
		}
	}

	@Override
	public int updateIn(MaterielDetailDO detailDO, PurchaseOrdersDO purchaseDO) throws ManagerException {
		int flag = materielDetailDAO.updateQuantity(detailDO);
		if (flag > 0) {
			purchaseDO.setMaterielDetailId(detailDO.getId());
			purchaseOrdersDAO.insertPurchaseOrders(purchaseDO);

			materielDAO.updateQuantity(detailDO.getMaterielId(), detailDO.getQuantity(), detailDO.getUpdatedBy());
		}
		return flag;
	}

	@Override
	public int updateOut(MaterielDetailDO detailDO, PaymentDO payDO, SellOrdersDO sellDO) throws ManagerException {
		int flag = materielDetailDAO.updateQuantity(detailDO);
		if (flag > 0) {
			sellDO.setMaterielDetailId(detailDO.getId());
			sellOrdersDAO.insertSellOrders(sellDO);
			materielDAO.updateQuantity(detailDO.getMaterielId(), detailDO.getQuantity(), detailDO.getUpdatedBy());

			paymentDAO.insertPayment(payDO);
		}
		return flag;
	}

	@Override
	public int updateQuantity(MaterielDetailDO detailDO) throws ManagerException {
		int flag = materielDetailDAO.updateQuantity(detailDO);
		if (flag > 0) {
			materielDAO.updateQuantity(detailDO.getMaterielId(), detailDO.getQuantity(), detailDO.getUpdatedBy());
		}
		return flag;
	}

}
